package com.wutianyi.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.ParserException;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.wutianyi.html.HtmlParser;
import com.wutianyi.html.impl.SinaInfoHandle;
import com.wutianyi.utils.HtmlUtils;
import com.wutianyi.utils.Pair;

public class FetchBlogUrlServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8145865662327505583L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String url = req.getParameter("url");
		if (StringUtils.isBlank(url)) {
			return;
		}
		url = URLDecoder.decode(url, "utf-8");
		String encoding = req.getParameter("encoding");

		String dealNext = req.getParameter("dealNext");

		if (StringUtils.isBlank(encoding)) {
			encoding = Charset.defaultCharset().name();
		}

		String content = HtmlUtils.getBodyString(url, encoding);

		try {
			HtmlParser parser = new HtmlParser(content);
			SinaInfoHandle handle = new SinaInfoHandle();
			List<Pair<String, String>> results = parser.parser(handle,
					new NodeFilter[] { new TagNameFilter("a"),
							new HasParentFilter(new TagNameFilter("td")) });
			if (StringUtils.isNotBlank(dealNext)) {
				Queue queue = QueueFactory.getQueue("blog-parse");
				for (String next : handle.getNextPages()) {
					TaskOptions to = TaskOptions.Builder
							.withUrl("/fetchblogurl");
					to.method(TaskOptions.Method.GET);
					to.param("encoding", encoding);
					to.param("url", URLEncoder.encode(next, "utf-8"));
					queue.add(to);
				}
			}
			Queue urlQueue = QueueFactory.getQueue("url-fetch");
			for (Pair<String, String> pair : results) {
				TaskOptions to = TaskOptions.Builder.withUrl("/fetch");
				to.method(TaskOptions.Method.GET);
				to.param("url", URLEncoder.encode(pair.getFirst(), "utf-8"));
				to.param("name", "file/rss/" + pair.getSecond() + ".xml");
				urlQueue.add(to);
			}
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
